package com.hg.library.os;

import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build;
import android.os.RecoverySystem;
import android.os.SystemProperties;
import android.util.Log;
import android.widget.Toast;

import com.hg.library.config.SystemProperty;
import com.hg.library.widget.HiToast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;

/**
 * Created by ASUS-PC on 2016/8/4.
 */
public class System {

    public static final String SYSTEM_UPDATE_PATH = "/data/media/0/update.zip";

    public static final class Device {

        public static final int MONITOR_UNKNOW = -1;
        public static final int MONITOR_H2 = 0;
        public static final int MONITOR_DH1 = 1;

        public static void setDeviceName(String name) {

        }

        public static String getDeviceName() {
            return Build.MODEL;
        }

        public static void setWifiDeviceName(String name) {
            SystemProperties.set("net.hostname", name);
        }

        public static String getWifiDeviceName() {
            return SystemProperties.get("net.hostname", Build.UNKNOWN);
        }

        public static void setBluetoothDeviceName(String name) {
            BluetoothAdapter.getDefaultAdapter().setName(name);
        }

        public static String getBluetoothDeviceName() {
            return BluetoothAdapter.getDefaultAdapter().getName();
        }

        /*
            用于判断是否为登陆状态
         */
        public static void setLoginStatus(String status) {
            SystemProperties.set("persist.sys.mLogin", status);
        }

        public static boolean getLoginStatus() {
            return SystemProperties.getBoolean("persist.sys.mLogin", false);
        }

        public static final int getMonitorModel() {
            File file = new File("sys/class/display/HDMI/", "monitor_name");
            if (file.exists()) {
                int deviceName = MONITOR_UNKNOW;
                try {
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                    String monitorName = bufferedReader.readLine();
                    if ("HighglassH2-1".equals(monitorName)) {
                        deviceName = MONITOR_H2;
                    } else if ("Toshiba-UH2D".equals(monitorName)) {
                        deviceName = MONITOR_DH1;
                    }

                } catch (FileNotFoundException e) {
                    Log.d("getMonitorName", e.fillInStackTrace().toString());
                } catch (IOException e) {
                    Log.d("getMonitorName", e.fillInStackTrace().toString());
                }

                return deviceName;
            } else {
                return MONITOR_UNKNOW;
            }
        }

        public static final String getMonitorName(){
            File file = new File("sys/class/display/HDMI/", "monitor_name");
            if (file.exists()) {
                try {
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                    String monitorName = bufferedReader.readLine();
                    if (monitorName != null){
                        return monitorName;
                    }
                } catch (FileNotFoundException e) {
                    Log.d("getMonitorName", e.fillInStackTrace().toString());
                } catch (IOException e) {
                    Log.d("getMonitorName", e.fillInStackTrace().toString());
                }
            }
            return "UNKNOWN";
        }
    }

    public static final boolean update(final Context context) {

        try {
            final File file = new File(System.SYSTEM_UPDATE_PATH);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    SystemProperties.set("media.rkffplayer.ready", "false");
                    SystemProperties.set("media.rkffplayer.mode", SystemProperties.get("media.rkffplayer.mode.default"));
                    while (true) {
                        try {
                            boolean ready = SystemProperties.getBoolean("media.rkffplayer.ready", false);
                            if (!ready) {
                                Thread.sleep(100);
                            } else {
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

            RecoverySystem.verifyPackage(file, new RecoverySystem.ProgressListener() {
                @Override
                public void onProgress(int progress) {
                    if (progress == 100) {
                        try {
                            RecoverySystem.installPackage(context, file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, null);

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static final class Attribute {
        public static final String SN = SystemProperties.get("ro.boot.serialno");

        public static final String VERSION_CODE = SystemProperties.get("ro.product.version");
    }

    public static long clearMemory(Context mContext) {
        return clearMemory(mContext, ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND, null);
    }

    public static long clearMemory(Context mContext, List<String> filterList) {
        return clearMemory(mContext, ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND, filterList);
    }

    public static long clearMemory(Context mContext, int importance) {
        return clearMemory(mContext, importance, null);
    }

    public static long clearMemory(Context mContext, int importance, List<String> filterList) {

        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        //目前可用内存
        long beforeLarge = memoryInfo.availMem;

        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
        for (int i = 0; i < runningAppProcesses.size(); i++) {
            ActivityManager.RunningAppProcessInfo runningAppProcessInfo = runningAppProcesses.get(i);
            //判断当进程等级，值越小，等级越高
            if (runningAppProcessInfo.importance > importance) {
                String[] pkgList = runningAppProcessInfo.pkgList;
                for (int j = 0; j < pkgList.length; j++) {
                    if (filterList != null) {
                        if (filterList.contains(pkgList[j])) continue;
                    }
                    activityManager.killBackgroundProcesses(pkgList[j]);
                }
            }
        }

        activityManager.getMemoryInfo(memoryInfo);
        //目前可用内存
        long afterLarge = memoryInfo.availMem;
        //用现在的可用内存减去没有清理之前的可用内存等于清理了多少内存
        return afterLarge - beforeLarge;
    }
}
